//
//  MainTabBar.swift
//  sinaweibo
//
//  Created by peter on 15/12/8.
//  Copyright © 2015年 peter. All rights reserved.
//

import UIKit

class MainTabBar: UITabBar {

    override init(frame: CGRect) {
        super.init(frame: frame)
        //设置ui
        setUpUI()
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setUpUI()
    }
    private func setUpUI() {
     addSubview(commoseBtn)
    }
    //给当前所有的子视图设置frame
    override func layoutSubviews() {
        super.layoutSubviews()
        print(subviews)
        //定义frame
        let w = bounds.width / 5
        let h = bounds.height
        let rect = CGRect(x: 0, y: 0, width: w, height: h)
        var index: CGFloat = 0
        for subView in subviews{
            if subView.isKindOfClass(NSClassFromString("UITabBarButton")!){
            subView.frame = CGRectOffset(rect, w * index, 0)
                index += ( index == 1) ? 2 : 1
                
            }
        }
        //设置按钮frame
        commoseBtn.frame = CGRectOffset(rect, w * 2, 0)
        bringSubviewToFront(commoseBtn)
    }
    lazy var commoseBtn: UIButton = {
        let btn = UIButton()
        //设置按钮的不同状态

        //设置按钮不同状态的图片与背景图片
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
         btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        //设置按钮的大小
        btn.sizeToFit()
      return btn
    }()
}
